package com.atguigu.guli.service.ucenter.controller.api;


import com.atguigu.guli.service.base.handler.JwtHelper;
import com.atguigu.guli.service.base.handler.JwtInfo;
import com.atguigu.guli.service.base.model.dto.MemberDto;
import com.atguigu.guli.service.base.result.R;
import com.atguigu.guli.service.ucenter.entity.Member;
import com.atguigu.guli.service.ucenter.entity.form.MemberRegistForm;
import com.atguigu.guli.service.ucenter.service.MemberService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 会员表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-06-02
 */
@RestController

@RequestMapping("/api/ucenter/member")
public class ApiMemberController {
    @Autowired
    MemberService memberService;

    //4、根据用户id查询用户信息
    @GetMapping("get-memberinfo/{id}")
    public R getMemberInfo(@PathVariable String id){
        Member member = memberService.getById(id);
        MemberDto memberDto = new MemberDto();
        BeanUtils.copyProperties(member ,memberDto );
        //微服务远程访问时，两个服务都需要使用的bean，可以抽取到公共模块DTO包中
        return R.ok().setData("item" , memberDto);
    }
    //3、解析token，返回用户登录信息
    @GetMapping("get-userinfo")
    public R getUserinfo(HttpServletRequest request){
        JwtInfo info = JwtHelper.getJwtInfo(request);//如果请求头中有token，并且数据没有被篡改、过期 则解析数据正常返回
        return R.ok().setData("item" , info);
    }
    //1、注册
    @PostMapping("regist")
    public R regist(@RequestBody  MemberRegistForm memberRegistForm){
        memberService.regist(memberRegistForm);
        return R.ok();
    }
    //2、登录：认证， 处理登录请求返回登录成功信息的jwt
    @PostMapping("login")
    public R login(@RequestParam  String mobile  , @RequestParam String password){
        String jwt = memberService.login(mobile , password);
        return R.ok().setData("jwt" , jwt);
    }

}

